package com.education.wisdom.controller.management;

import com.education.common.utils.ApiReturnObject;
import com.education.common.utils.DateUtil;
import com.education.wisdom.feign.UserFeign;
import com.education.wisdom.pojo.JrMoments;
import com.education.wisdom.pojo.Message;
import com.education.wisdom.service.IJrMomentsService;
import com.education.wisdom.service.MessageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/manage/memont")
public class MomentsController {


    @Autowired
    private IJrMomentsService momentsService;

    @Autowired
    private UserFeign userFeign;

    @Autowired
    private MessageService messageService;

    /**
     * 管理端-APP管理-达人直播管理
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    public ApiReturnObject list(Integer pageNum, Integer pageSize) {
        if (pageNum != null && pageSize != null){
            PageHelper.startPage(pageNum,pageSize);
        }
        List<JrMoments> jrMoments = momentsService.selectJrMomentsList(null);
        for (JrMoments jrMoment : jrMoments) {
            Map<String, Object> map = userFeign.selectByUserId(Integer.parseInt(jrMoment.getUserId()));
            if (map != null && !map.isEmpty()) {
                jrMoment.setSysRelName(map.get("name").toString());
                jrMoment.setPhone(map.get("phone").toString());
            }
            jrMoment.setImageList(jrMoment.getImages().split(","));
        }
        PageInfo<JrMoments> info = new PageInfo<>(jrMoments);
        return new ApiReturnObject("200", "查询成功", info);
    }

    /**
     *
     * @param jrMoments
     * @return
     */
    @PostMapping("/update")
    public ApiReturnObject update(@RequestBody JrMoments jrMoments) {
        jrMoments.setDeflag("1");
        int i = momentsService.updateJrMoments(jrMoments);

        JrMoments moments = momentsService.selectJrMomentsById(jrMoments.getId());
        Message message = new Message();
        StringBuilder builder = new StringBuilder();
        message.setTitle(builder.append("课程下线通知 : 您在").append(DateUtil.dataTimeFormat(moments.getCreateTime())).append("在职业达人直播中发布的内容, 违反平台规定, 已被下线处理").toString());
        message.setType("3");
        message.setCreatetime(new Date());
        message.setUserId(Integer.parseInt(moments.getUserId()));
        message.setIsRead("1");
        messageService.insertMessage(message);
        //查询消息推送
        Integer count = messageService.selectCount(moments.getUserId());
        if (i > 0){
            return new ApiReturnObject("200", "修改成功", "修改成功");
        }
         return new ApiReturnObject("200", "修改失败", "修改失败");
    }

}
